package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.BaseContext;
import com.itheima.youdianda.util.MyPage;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.ArticleDto;
import com.itheima.youdianda.dto.ArticleReceive;
import com.itheima.youdianda.entity.YddAdvimg;
import com.itheima.youdianda.entity.YddArticle;
import com.itheima.youdianda.entity.YddCate;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import com.itheima.youdianda.util.MyPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.dc.pr.PRError;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddCateService yddCateService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @GetMapping("/list")
    public R<MyPage> list(ArticleReceive articleReceive) {
        MyPage<ArticleDto> articleDtoPage = new MyPage<>();
        Page<YddArticle> articlePage = new Page(Integer.valueOf(articleReceive.getPage()), Integer.valueOf(articleReceive.getLimit()));
        //进行条件判断
        LambdaQueryWrapper<YddArticle> articleQueryWrapper = new LambdaQueryWrapper<>();
        articleQueryWrapper.orderByDesc(YddArticle::getId);
        articleQueryWrapper.like(StringUtils.isNotEmpty(articleReceive.getTitle()), YddArticle::getTitle, articleReceive.getTitle());
        articleQueryWrapper.eq(articleReceive.getCateid() != null, YddArticle::getCateid, articleReceive.getCateid());
        articleQueryWrapper.between(StringUtils.isNotEmpty(articleReceive.getStart_time()) && StringUtils.isNotEmpty(articleReceive.getEnd_time()),
                YddArticle::getCreate_date, articleReceive.getStart_time(), articleReceive.getEnd_time());
        yddArticleService.page(articlePage, articleQueryWrapper);
        List<YddArticle> articleRecords = articlePage.getRecords();
        //往MyPage中传输数据
        articleDtoPage.setCurrentPage(Long.valueOf(articleReceive.getPage()));
        articleDtoPage.setCount(articlePage.getTotal());
        articleDtoPage.setTotalPages(articlePage.getPages());
        articleDtoPage.setPageSize(Long.valueOf(articleReceive.getLimit()));
        List<ArticleDto> articleDtoRecords = new ArrayList<>();
        for (YddArticle item : articleRecords) {
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(item, articleDto);
            LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YddCate::getId, item.getCateid());
            YddCate one = yddCateService.getOne(queryWrapper);
            articleDto.setCatename(one.getCatename());
            articleDtoRecords.add(articleDto);
        }
        articleDtoPage.setData(articleDtoRecords);
        return R.success(articleDtoPage);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody YddArticle yddArticle) {
        yddArticle.setUser_id(Integer.valueOf(request.getHeader("x-token")));
        yddArticleService.save(yddArticle);
        return R.success("");
    }

    @GetMapping("/rowInfo")
    public R<YddArticle> rowInfo(String id) {
        log.info("ID:{}", id);
        YddArticle byId = yddArticleService.getById(id);
        return R.success(byId);
    }

    @GetMapping("del")
    public R<String> delete(String id) {
        LambdaQueryWrapper<YddAdvimg>queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(YddAdvimg::getAdvimgpos,id);
        yddAdvimgService.remove(queryWrapper);
        yddArticleService.removeById(id);
        return R.success("删除成功");
    }

    @PostMapping("/update")
    public R<String> update(@RequestBody YddArticle yddArticle) {
        log.info("文章修改传过来的内容:{}", yddArticle);
        yddArticleService.updateById(yddArticle);
        return R.success("修改成功");
    }
}
